package cn.clementine.core.api;

import cn.clementine.core.crud.DataBaseCrud;
import cn.clementine.core.entity.db.DatabaseInfo;
import cn.clementine.core.pojo.DatabaseCreateInfo;
import cn.clementine.core.svc.DatabaseService;
import cn.clementine.core.vo.Result;
import cn.dev33.satoken.annotation.SaCheckLogin;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@SaCheckLogin
@RequestMapping("/db")
@Slf4j
public class DBApi {
    @Resource
    DatabaseService databaseService;
    @Resource
    DataBaseCrud dataBaseCrud;
    @GetMapping("/query")
    public Result query() {
        String url = "jdbc:mysql://124.223.53.154:3306/ll_test";
        String username = "ll_test";
        String password = "xbmh5Sk64TN2G5ws";
        String sql = "SELECT * FROM mytable";
        List<Map<String, Object>> result = databaseService.executeQuery(url, username, password, sql);
        return Result.ok(result);
    }
    @PostMapping("/create_database")
    public Result createDatabase(@RequestBody DatabaseCreateInfo info) {
        String url = "your_db_url";
        String username = "your_username";
        String password = "your_password";
        String result = databaseService.createDatabase(url, username, password, info);
        return Result.ok(result);
    }
    @GetMapping("/all_db")
    public Result getAllDatabase(){
        return Result.ok(dataBaseCrud.findAll());
    }
    @PostMapping("/add")
    public Result addDatabase(@RequestBody DatabaseInfo newDatabase) {
        DatabaseInfo savedDatabase = dataBaseCrud.save(newDatabase);
        return Result.ok(savedDatabase);
    }
}
